﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace EBA.Data
{
    /// <summary>
    /// 实例主键变更跟踪器
    /// </summary>
    public interface IEntityKeyChangeTracker
    {
    }

    /// <summary>
    /// 实例主键变更跟踪器
    /// </summary>
    public class EntityKeyChangeTracker<TEntity> : IEntityKeyChangeTracker
    {
        /// <summary>
        /// 主键值删除事件
        /// </summary>
      public event EntityKeyDeletingEventHandler<TEntity> EntityKeyDeleting;

      /// <summary>
      /// 主键值删除事件
      /// </summary>
      public event EntityKeysDeletingEventHandler<TEntity> EntityKeysDeleting;

      /// <summary>
      /// 主键值修改事件
      /// </summary>
      public event EntityKeyChangingEventHandler<TEntity> EntityKeyChanging;
    

        /// <summary>
        /// 删除主键值
        /// </summary>
        /// <param name="context">事务上下文</param>
        /// <param name="key">主键值</param>
      public void OnEntityKeyDeleting(IUnitOfWork context, object key)
      {
          if (this.EntityKeyDeleting != null)
          {
              this.EntityKeyDeleting(context, key);
          }
      }

      /// <summary>
      /// 删除主键值
      /// </summary>
      /// <param name="context">事务上下文</param>
      /// <param name="query">对象查询器</param>
      /// <param name="filter">主键值查询条件</param>
      public void OnEntityKeysDeleting(IUnitOfWork context, IQuery<TEntity> query, Expression<Func<TEntity, bool>> filter)
      {
          if (this.EntityKeysDeleting != null)
          {
              this.EntityKeysDeleting(context, query, filter);
          }
      }


      /// <summary>
      /// 修改主键值
      /// </summary>
      /// <param name="context">事务上下文</param>
      /// <param name="query">对象查询器</param>
      /// <param name="originalKey">旧主键值</param>
      /// <param name="currentKey">新主键值</param>
      public void OnEntityKeyChanging(IUnitOfWork context,IQuery<TEntity> query, object originalKey, object currentKey)
      {
          if (this.EntityKeyChanging != null)
          {
              this.EntityKeyChanging(context,query, originalKey, originalKey);
          }
      }
    }

    /// <summary>
    /// 主键删除委托
    /// </summary>
    /// <typeparam name="TEntity">对应类型</typeparam>
    /// <param name="context">数据上下文</param>
    /// <param name="key">主键值</param>
    public delegate void EntityKeyDeletingEventHandler<TEntity>(IUnitOfWork context, object key);
    /// <summary>
    /// 主键修改委托
    /// </summary>
    /// <typeparam name="TEntity">对应类型</typeparam>
    /// <param name="context">数据上下文</param>
    /// <param name="query">对象查询器</param>
    /// <param name="originalKey">旧主键值</param>
    /// <param name="currentKey">新主键值</param>
    public delegate void EntityKeyChangingEventHandler<TEntity>(IUnitOfWork context, IQuery<TEntity> query, object originalKey, object currentKey);
    /// <summary>
    /// 主键删除委托
    /// </summary>
    /// <typeparam name="TEntity">对应类型</typeparam>
    /// <param name="context">数据上下文</param>
    /// <param name="query">对象查询提供者</param>
    /// <param name="filter">主键查询条件</param>
    public delegate void EntityKeysDeletingEventHandler<TEntity>(IUnitOfWork context, IQuery<TEntity> query, Expression<Func<TEntity, bool>> filter);


}
